package com.cowain.system.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cowain.base.core.domain.entity.SysRoleMenuEntity;
import com.cowain.base.core.domain.vo.SysRoleVo;
import com.cowain.system.mapper.SysRoleMenuMapper;
import com.cowain.system.service.SysRoleMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author songxz
 * @date 2022/11/14
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenuEntity> implements SysRoleMenuService {

    /**
     * 插入角色和按钮关系
     *
     * @param role
     * @return
     */
    @Override
    public boolean insertRoleMenu(SysRoleVo role) {
        boolean insertFlag = true;
        Long[] menuIds  = role.getMenuIds();
        if(ArrayUtil.isNotEmpty(menuIds)){
            List<SysRoleMenuEntity> roleMenuList = new ArrayList<>();
            for (Long menuId:menuIds) {
                SysRoleMenuEntity entity = SysRoleMenuEntity
                        .builder()
                        .menuId(menuId)
                        .roleId(role.getRoleId())
                        .build();
                roleMenuList.add(entity);
            }
            insertFlag = this.saveBatch(roleMenuList);
        }
        return insertFlag;
    }
}
